package com.triadsystems.cashweb.modelo.persistencia.entidades;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import lombok.Getter;
import lombok.Setter;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table 
@SequenceGenerator(name="seq_usuario", sequenceName = "cw_seq_usuario", allocationSize = 2, initialValue = 1)
public class Usuario extends ValueObject implements UserDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7385637985030779435L;

	@Id
	@Column
	@GeneratedValue(generator = "seq_usuario", strategy = GenerationType.SEQUENCE)
	@Getter @Setter
	private Long entityId;
	
	@Getter @Setter
	private String email;
	
	@Getter @Setter
	private String senha;

	@Getter @Setter
	private String nome;

	@Getter @Setter
	private Boolean isAtivo = true;
	
	@Getter
	@Setter
	@Lob
	@Basic(fetch = FetchType.EAGER)
	private byte[] imagem;

	@Getter
	@Setter
	private String nomeImagem;
	
	@Getter @Setter
	@ManyToOne
	private Filial filial;
	
	@Getter @Setter
	@Transient
	private String perfil = "";

	@Transient
	private Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

	@Override
	@Transient
	public Collection<GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Transient
	public boolean isAccountNonExpired() {
		return this.isAtivo;
	}

	@Transient
	public boolean isAccountNonLocked() {
		return this.isAtivo;
	}

	@Transient
	public boolean isCredentialsNonExpired() {
		return this.isAtivo;
	}

	@Transient
	public boolean isEnabled() {
		return this.isAtivo;
	}

	@Override
	@Transient
	public String getPassword() {
		return senha;
	}

	@Override
	@Transient
	public String getUsername() {
		return this.email;
	}

}
